/*
 * Test program for 8x8 LED matrix (single color) and two shift registor
 * test No.4
 *
 * HISTORY
 * 2010/8/24  New
 *
 */

/*
 * Copyright (c) 2010 elekid
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */

// for 7 segments x 4 display

int CLOCK_PIN_K =2;
int DATA_PIN_K  =3;

int CLOCK_PIN_A =4;
int DATA_PIN_A  =5;

byte PATTERN[40][8] = {
{
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011
} ,
{
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011
} ,
{
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111
} ,
{
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111
} ,
{
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111
} ,
{
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111
} ,
{
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111
} ,
{
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111
} ,
{
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011
} ,
{
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011
} ,
{
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111
} ,
{
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111
} ,
{
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111
} ,
{
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111
} ,
{
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111
} ,
{
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111
} ,
{
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011
} ,
{
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011
} ,
{
B11101111,
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111
} ,
{
B11011111,
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111
} ,
{
B11011111,
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111
} ,
{
B11101111,
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111
} ,
{
B11110111,
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111
} ,
{
B11111011,
B11111011,
B11110111,
B11101111,
B11011111,
B11011111,
B11101111,
B11110111
} ,
{
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100
} ,
{
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100
} ,
{
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000
} ,
{
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000
} ,
{
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000
} ,
{
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000
} ,
{
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000
} ,
{
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000
} ,
{
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100
} ,
{
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100
} ,
{
B00010000,
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000
} ,
{
B00100000,
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000
} ,
{
B00100000,
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000
} ,
{
B00010000,
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000
} ,
{
B00001000,
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000
} ,
{
B00000100,
B00000100,
B00001000,
B00010000,
B00100000,
B00100000,
B00010000,
B00001000
} 
};


#define WAITS 1
#define LOOPMAX 5


void setup()
{
  pinMode(CLOCK_PIN_K, OUTPUT);
  pinMode(CLOCK_PIN_A, OUTPUT);
  pinMode(DATA_PIN_K,  OUTPUT);
  pinMode(DATA_PIN_A,  OUTPUT);
}
void loop()
{ 
  int buffer[8];

  /*
  for ( int i=0 ; i<8 ; i++) {
    buffer[i]= PATTERN[i];
  }
  */

  for (int step=0 ; step < 40 ; step++) {
    // 
    for ( int disp_cnt = 0 ; disp_cnt < LOOPMAX ; disp_cnt++) {
      for (int row=0 ; row < 8 ; row++) {
        shiftOut(DATA_PIN_K, CLOCK_PIN_K, MSBFIRST, 0XFF);
        nextRow();
        byte b = PATTERN[step][row];
        shiftOut(DATA_PIN_K, CLOCK_PIN_K, MSBFIRST, b);
     
        delay(WAITS);

      } // row
    } // disp_cnt
  } // step
}    

void matrix_clear(){
  // not implemented.
}

void matrix_on(int i,int j) {
  byte b=0xFF ^ 1 << j;
  shiftOut(DATA_PIN_K, CLOCK_PIN_K, MSBFIRST, b);
  if ( i == 0) {
    b = 1 ;
  } else {
    b = 1 << i;
  }
  shiftOut(DATA_PIN_K, CLOCK_PIN_K, MSBFIRST, b);
  
}

void nextRow() 
{
  static int cnt=0;
  
  if (cnt == 0 ) {
    digitalWrite(DATA_PIN_A, HIGH);
  } else {
    digitalWrite(DATA_PIN_A, LOW);
  }

  digitalWrite(CLOCK_PIN_A, HIGH);
  digitalWrite(CLOCK_PIN_A, LOW);
  
  cnt++ ;
  if (cnt >= 8 ) 
  {
    cnt=0;
  }
}

/*
  wiring_shift.c - shiftOut() function
  Part of Arduino - http://www.arduino.cc/
  :
  :
  :
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val)
{
  int i;
  for (i = 0; i < 8; i++)  {
    if (bitOrder == LSBFIRST)
      digitalWrite(dataPin, !!(val & (1 << i)));
    else  
      digitalWrite(dataPin, !!(val & (1 << (7 - i))));
      
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);    
  }
}


*/
